package com.dic.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter {
	private String charaterEncoding;
	private boolean enabled;
	public void init(FilterConfig config) throws ServletException {
		charaterEncoding = config.getInitParameter("charaterEncoding");
		enabled = "true".equalsIgnoreCase(config.getInitParameter("enabled"));
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		if(enabled || charaterEncoding != null) {
			request.setCharacterEncoding(charaterEncoding);
			response.setCharacterEncoding(charaterEncoding);
		}
		chain.doFilter(request, response);
	}
	public void destroy() {
		charaterEncoding = null;
	}
}
